%% Copyright
-module(http_util).
-author("").

%% API
-export([get_paging_parameters/1, get_product_not_found_response/3, get_review_not_found_response/3]).

get_paging_parameters(Req) ->
  {PageIndexVal, Req1} = cowboy_req:qs_val(<<"pageIndex">>, Req, <<"0">>),
  {PageSizeVal, Req2} = cowboy_req:qs_val(<<"pageSize">>, Req1, <<"10">>),

  PageIndex =
  try
    binary_util:binary_to_integer(PageIndexVal)
  catch
    _:_ -> 0
  end,

  PageSize =
  try
    binary_util:binary_to_integer(PageSizeVal)
  catch
    _:_ -> 10
  end,
  {{PageIndex, PageSize}, Req2}.

get_product_not_found_response(Req, State, ProductId) ->
  RespBody = jsx:encode([{<<"Message">>, <<"Product Id ", ProductId/binary, " not found">>}]),
  {ok, Req1} = cowboy_req:reply(404, [{<<"content-type">>, <<"application/json">>}], RespBody, Req),
  {ok, Req1, State}.

get_review_not_found_response(Req, State, ReviewId) ->
  RespBody = jsx:encode([{<<"Message">>, <<"Review Id ", ReviewId/binary, " not found">>}]),
  {ok, Req1} = cowboy_req:reply(404, [{<<"content-type">>, <<"application/json">>}], RespBody, Req),
  {ok, Req1, State}.
